// pages/account/teaching/teaching.js
let common = require('../../../utils/common.js');
const conf = {
  data: {
    hasEmptyGrid: false
  },
  onLoad(options) {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_day = date.getDate();
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.setData({
      cur_year,
      cur_month,
      cur_day,
      weeks_ch
    });
    this.reqTeaching(cur_year + '-' + cur_month + '-' + cur_day);
  },
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  calculateDays(year, month) {
    let days = [];
    const thisMonthDays = this.getThisMonthDays(year, month);
    for (let i = 1; i <= thisMonthDays; i++) {
      days.push(i);
    }
    this.setData({
      days
    });
  },
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;
    const cur_year = this.data.cur_year;
    const cur_month = this.data.cur_month;
    if (handle === 'prev') {
      let newMonth = cur_month - 1;
      let newYear = cur_year;
      if (newMonth < 1) {
        newYear = cur_year - 1;
        newMonth = 12;
      }
      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);
      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })
    } else {
      let newMonth = cur_month + 1;
      let newYear = cur_year;
      if (newMonth > 12) {
        newYear = cur_year + 1;
        newMonth = 1;
      }
      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);
      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      });
    }
    this.reqTeaching(cur_year + '-' + cur_month + '-' + this.data.cur_day);
  },
  handleDay: function (e) {
    let that = this, idx = e.currentTarget.dataset.idx;
    var day = that.data.days[idx];
    var startTime = that.data.cur_year + '-' + that.data.cur_month + '-' + day;
    console.log(startTime);
    that.setData({
      cur_day:day
    });
    that.reqTeaching(startTime);
  },
  reqTeaching: function (startTime) {
    let that = this;
    common.asynPost(common.API.user.teaching, {
      startTime: startTime
    }, function (data) {
      if (data['teaching'] && data['teaching'].length) {
        that.setData({
          teaching: data['teaching']
        });
      }
    });
  },
  onShareAppMessage() {
    // return {
    //   title: '小程序日历',
    //   desc: '还是新鲜的日历哟',
    //   path: 'pages/index/index'
    // }
  }
};
Page(conf);
